Skip to content

fix: Trim DSN string before URI parsing#5113

Merged
adinauer merged 7 commits intomainfrom
fix/trim-dsn-whitespace
Mar 2, 2026
Merged

fix: Trim DSN string before URI parsing#5113
adinauer merged 7 commits intomainfrom
fix/trim-dsn-whitespace

Conversation

@adinauer
Copy link
Member

@adinauer adinauer commented Feb 23, 2026

Trailing or leading whitespace in the DSN string (commonly introduced by copy-paste from dashboards, wikis, or config tools) causes a URISyntaxException that crashes the application on startup.

This trims the DSN before passing it to the URI constructor in Dsn.java.

Fixes #5087

Trailing or leading whitespace in the DSN string (commonly introduced
by copy-paste) causes a URISyntaxException that crashes the application
on startup. Trim the DSN before passing it to the URI constructor.

Fixes GH-5087
Co-Authored-By: Claude <noreply@anthropic.com>
@adinauer adinauer changed the title fix(options): Trim DSN string before URI parsing fix: Trim DSN string before URI parsing Feb 23, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 23, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (otel) Create sentry-opentelemetry-otlp module for combining OpenTelemetry SDK OTLP export with Sentry SDK by adinauer in #5100
  • (screenshot) Add screenshot masking using view hierarchy by romtsn in #5077

Bug Fixes 🐛

  • Trim DSN string before URI parsing by adinauer in #5113
  • Safe unregister SystemEventsBroadcastReceiver by kollesnica1337 in #5106

Internal Changes 🔧

Deps

  • Bump getsentry/craft from 2.21.7 to 2.23.1 by dependabot in #5129
  • Update Native SDK to v0.13.1 by github-actions in #5104
  • Bump actions/upload-artifact from 6 to 7 by dependabot in #5130
  • Bump actions/download-artifact from 7 to 8 by dependabot in #5132
  • Bump gradle/actions from 5.0.1 to 5.0.2 by dependabot in #5131
  • Bump github/codeql-action from 4.32.2 to 4.32.4 by dependabot in #5109
  • Bump getsentry/craft from 2.21.2 to 2.21.7 by dependabot in #5110

🤖 This preview updates automatically when you update the PR.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions
Copy link
Contributor

github-actions bot commented Feb 23, 2026

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 318.17 ms 357.80 ms 39.64 ms
Size 1.58 MiB 2.29 MiB 723.29 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
e59e22a 329.74 ms 383.31 ms 53.57 ms
fc5ccaf 270.49 ms 363.90 ms 93.41 ms
d15471f 361.89 ms 378.07 ms 16.18 ms
d15471f 322.58 ms 396.08 ms 73.50 ms
abfcc92 304.04 ms 370.33 ms 66.29 ms
91bb874 310.68 ms 359.24 ms 48.56 ms
ab8a72d 316.24 ms 356.38 ms 40.14 ms
d15471f 286.65 ms 314.68 ms 28.03 ms
6727e14 337.22 ms 373.94 ms 36.71 ms
f064536 329.00 ms 395.62 ms 66.62 ms

App size

Revision Plain With Sentry Diff
e59e22a 1.58 MiB 2.20 MiB 635.34 KiB
fc5ccaf 1.58 MiB 2.13 MiB 557.54 KiB
d15471f 1.58 MiB 2.13 MiB 559.54 KiB
d15471f 1.58 MiB 2.13 MiB 559.54 KiB
abfcc92 1.58 MiB 2.13 MiB 557.31 KiB
91bb874 1.58 MiB 2.13 MiB 559.07 KiB
ab8a72d 1.58 MiB 2.12 MiB 551.55 KiB
d15471f 1.58 MiB 2.13 MiB 559.54 KiB
6727e14 1.58 MiB 2.28 MiB 718.64 KiB
f064536 1.58 MiB 2.20 MiB 633.90 KiB

Previous results on branch: fix/trim-dsn-whitespace

Startup times

Revision Plain With Sentry Diff
7a140bb 308.28 ms 359.77 ms 51.49 ms
2878e08 305.85 ms 364.33 ms 58.47 ms

App size

Revision Plain With Sentry Diff
7a140bb 1.58 MiB 2.29 MiB 719.80 KiB
2878e08 1.58 MiB 2.29 MiB 723.30 KiB

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Whitespace-only DSN silently disables SDK instead of erroring
    • Updated SentryOptions.setDsn to preserve whitespace-only values so initialization validates them as invalid DSNs instead of treating them as intentional empty-string disablement, and added a regression test.

Create PR

Or push these changes by commenting:

@cursor push 0a7e3bd946
Preview (0a7e3bd946)
diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java
--- a/sentry/src/main/java/io/sentry/SentryOptions.java
+++ b/sentry/src/main/java/io/sentry/SentryOptions.java
@@ -749,7 +749,12 @@
    * @param dsn the DSN
    */
   public void setDsn(final @Nullable String dsn) {
-    this.dsn = dsn != null ? dsn.trim() : null;
+    if (dsn == null) {
+      this.dsn = null;
+    } else {
+      final String trimmedDsn = dsn.trim();
+      this.dsn = trimmedDsn.isEmpty() && !dsn.isEmpty() ? dsn : trimmedDsn;
+    }
     this.parsedDsn.resetValue();
 
     dsnHash = StringUtils.calculateStringHash(this.dsn, logger);

diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt
--- a/sentry/src/test/java/io/sentry/SentryTest.kt
+++ b/sentry/src/test/java/io/sentry/SentryTest.kt
@@ -341,6 +341,11 @@
   }
 
   @Test
+  fun `initializes Sentry with whitespace-only dsn, throwing IllegalArgumentException`() {
+    assertThrows(java.lang.IllegalArgumentException::class.java) { initForTest { it.dsn = "   " } }
+  }
+
+  @Test
   fun `captureUserFeedback gets forwarded to client`() {
     initForTest { it.dsn = dsn }
This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.

adinauer and others added 3 commits March 2, 2026 13:32
Previously an empty or whitespace-only DSN string would fall through
to the URI constructor, producing a confusing error message. Now the
Dsn constructor checks for empty strings after trimming and throws
an IllegalArgumentException with a clear message.

Co-Authored-By: Claude <noreply@anthropic.com>
@adinauer adinauer enabled auto-merge (squash) March 2, 2026 14:45
@adinauer adinauer merged commit b7fde00 into main Mar 2, 2026
65 of 68 checks passed
@adinauer adinauer deleted the fix/trim-dsn-whitespace branch March 2, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dsn parsing fails with trailing whitespace - should trim input before URI parsing

3 participants